<?php


namespace app\admin\controller;

use think\Controller;
use think\Db;

class Cases extends Base
{


    public function index()
    {

        if (request()->isAjax()) {

            $res = Db::name('cases')->order('id','desc')->select();
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/60x60/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
                $res[$index]['small_img'] = $qiniu_config['domain'] . '/' . $re['small_img'] . $q_thumb;

            }
            $this->success('ok', '', $res);

        } else {
            $count = Db::name('cases')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }

        // for ($i=0;$i<7;$i++){
        //     foreach ($res as $index => $re) {
        //         $data=$re;
        //         unset($data['id']);
        //         // var_dump($data);
        //         $gd->insertGetId($data);
        //     }
        // }

    }



    public function li()
    {

        if (request()->isAjax()) {

            $res = Db::name('cases')->order('id','desc')->select();
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/60x60/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                //$res[$index]['release_time'] = date('Y-m-d H:i:s', $re['release_time']);
                $res[$index]['small_img'] = $qiniu_config['domain'] . '/' . $re['small_img'] . $q_thumb;

            }
            $this->success('ok', '', $res);

        } else {
            $count = Db::name('cases')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }

        // for ($i=0;$i<7;$i++){
        //     foreach ($res as $index => $re) {
        //         $data=$re;
        //         unset($data['id']);
        //         // var_dump($data);
        //         $gd->insertGetId($data);
        //     }
        // }

    }

    //添加案例
    public function add_cases()
    {
        if (request()->isAjax()){
            $data= input('');
//            $data['release_time'] = time();
            if(!request()->has('content')){
                $this->error('缺少案例内容');
            }

            $shuju=$data['content'];
            $data['create_time']=time();
            unset($data['file']);
            unset($data['content']);
            // dump($data);
            $data_id = Db::name('cases')->insertGetId($data);

            $res=["cases_detail"=>"","cases_img"=>"","cases_id"=>""];
            foreach ($shuju as $index => $re){
                $res['cases_detail']=$re['cases_detail'];
                $res['cases_img']=$re['cases_img'];
                $res['cases_id']=$data_id;
                Db::name('cases_detail')->insert($res);
            }

            if ($data_id) {
                $this->success('操作成功');
            }
            $this->error('操作失败');

        }
        else{
            return $this->fetch();
        }

//        if (request()->isAjax()) {
//
//            $title = input('title');
//            if (!$title) {
//                $this->error('error');
//            }
//            $sl = Db::name('slide');
//            if ($sl->where('title', $title)->find()) {
//                $this->error('标题重复了');
//            }
//            $sl->insertGetId(['title' => $title]);
//            $this->success('操作成功');
//
//        } else {
//            return $this->fetch();
//        }
    }

    //添加案例中的案例详情
    public function add_cases_detail(){
        if (request()->isAjax()) {
            $data= input('');

            unset($data['file']);
            // dump($data);
            $res = Db::name('cases_detail')->insertGetId($data);


            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb="?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                $res[$index]['cases_img'] = $qiniu_config['domain'] . '/' . $re['cases_img'].$q_thumb;
            }

            $this->success('', '', $res);
        } else {
            return $this->fetch();
//            $this->assign('id', $id);
//            Db::name('cases_detail')->where('cases_id',$id)->find();
//            return $this->fetch();
        }
    }

    public function edit_cases(){
        if (request()->isAjax()) {
            $data = input('');
            unset($data['file']);

            $res = Db::name('cases')->where('id', $data['id'])->update($data);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');


        } else {
            $id = input('id');
            if (!$id) {
                $this->error('id 异常');
            }
            $res = Db::name('cases')->find($id);
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb="?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            $res['small_img2']=$qiniu_config['domain'] . '/' . $res['small_img'].$q_thumb;
            $this->assign('item', $res);
            return $this->fetch();
        }
    }

    //案例详情
    public function detail_cases()
    {

        if (request()->isAjax()) {
            $id = input('id');
            $res = Db::name('cases_detail')->where('cases_id',$id)->select();

            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb="?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                $res[$index]['cases_img'] = $qiniu_config['domain'] . '/' . $re['cases_img'].$q_thumb;
            }

            $this->success('', '', $res);
        } else {
            $id = input('id');
            $this->assign('id', $id);
            Db::name('cases_detail')->where('cases_id',$id)->find();
            return $this->fetch();
        }


    }


    public function pic_add()
    {
        $data = input('');
        unset($data['file']);
        Db::name('cases_detail')->insertGetId($data);
        $this->success('操作成功');
        // try {
        //     Db::name('slide_content')->insertGetId($data);
        //     $this->success();
        // } catch (Exception $exception) {
        //     $this->error();
        // }

    }


    public function pic_del()
    {
        $id = input('id');
        Db::name('cases_detail')->where('id',$id)->delete();
        $this->success('操作成功');
    }


    public function del()
    {
        $data = input('');
        $ids = $data['ids'];

        if (!$ids) {
            $this->error('异常');
        }

        Db::name('cases')->where('id', 'in', $ids)->delete();
        Db::name('cases_detail')->where('cases_id','in',$ids)->delete();

        // dump($sql);
        $this->success('操作成功');
    }

    //案例搜索
    public function cases_search()
    {

        $status[0] = '禁用';
        $status[1] = '正常';
        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 10;

        $key = trim(input('keyword'));
        if (!$key) {
            $this->error('没有内容');
        }

        $res = Db::name('cases')->page($page, $limit)
            ->where('cases_name', 'like', '%'.$key.'%')
//            ->where('title LIKE :key ', ['key' => "%$key%"])
            ->select();
        $qiniu_config = cmf_get_option('qiniu');
        // 七牛样式处理
        $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/60x60/quality/100/interlace/0";
        foreach ($res as $index => $re) {
            $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
            $res[$index]['small_img'] = $qiniu_config['domain'] . '/' . $re['small_img'] . $q_thumb;

        }
        //根据关键字返回 相应数据
        $total = Db::name('cases')
            //搜索相关
            ->where('cases_name', 'like', '%'.$key.'%')
//            ->where('title LIKE :key ', ['key' => "%$key%"])
            ->count();
        return json(['code' => 1, 'msg' => 'ok', 'total' => $total, 'data' => $res]);


    }

}
